﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
using System.Diagnostics;
using System.Collections.ObjectModel;

namespace BE.Web.Betfair.Interface.Entities
{
    /// <summary>
    /// A EventType like soccer, for which events can be retrieved.
    /// </summary>
    [DebuggerDisplay("{Id} - {Name}")]
    public sealed class BetEventType
    {
        /// <summary>
        /// Timestamp indicating when this beteventype was read
        /// </summary>
        /// <remarks>The timestamp can be important for caches, to consider wether this entity should be reloaded.</remarks>
        public DateTime LocalTimeStamp { get; private set; }
        /// <summary>
        /// Id 
        /// </summary>
        public int Id { get; private set; }
        
        /// <summary>
        /// The NextMarket
        /// </summary>
        public int NextMarket { get; private set; }


        /// <summary>
        /// NAme of the Event
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// Gets or sets the exchange id.
        /// </summary>
        /// <value>The exchange id.</value>
        public int ExchangeId { get; private set; }

        /// <summary>
        /// Events of the eventtsypes
        /// </summary>
        public ObservableCollection<BetEvent> Events { get; private set; }

        [ContractInvariantMethod]
        void ObjectInvariant()
        {
            Contract.Invariant(this.Name != null);
            Contract.Invariant(this.Events != null);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BetEventType"/> class.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="name">The name.</param>
        /// <param name="nextMarket">The next market.</param>
        /// <param name="exhangeId">The exhange id.</param>
        public BetEventType(int id, string name, int nextMarket, int exhangeId)
        {
            #region conditions
            Contract.Requires<ArgumentNullException>(name != null);
            Contract.Ensures(this.Name != null);
            Contract.Ensures(this.Events != null);
            #endregion
            this.Events = new ObservableCollection<BetEvent> ();
            this.LocalTimeStamp = DateTime.Now;
            this.Id = id;
            this.Name = name;
            this.NextMarket = nextMarket;
            this.ExchangeId = exhangeId;
        }


        #region overrides
        
        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return this.Name;
        }

        /// <summary>
        /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
        /// <returns>
        /// 	<c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public override bool Equals(object obj)
        {
            bool equal = false;

            if (obj != null && obj is BetEventType)
            {
                if (Object.ReferenceEquals(obj, this))
                {
                    equal = true;
                }
                else
                {
                    equal = ((BetEventType)obj).Id == this.Id;
                }
            }

            return equal;
        }

        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <returns>
        /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
        /// </returns>
        public override int GetHashCode()
        {
            return this.Id.GetHashCode();
        }
        #endregion
    }
}
